<%@ page trimDirectiveWhitespaces="true" %>
<%@page import="com.jason.imagesnode.MyStringUtil" %>
<%@page import="org.json.JSONObject" %>

<%@ page contentType="text/html; charset=UTF-8" %>

<%@ page import="java.io.File" %>
<%@ page import="java.io.BufferedReader" %>
<%@ page import="com.alibaba.fastjson.JSON" %>
<%@ page import="com.jason.imagesnode.WriteQrCode" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="org.apache.commons.lang.StringUtils" %>

<%
    response.setHeader("Access-Control-Allow-Origin", "*");

    JSONObject obj = new JSONObject();
    obj.put("result", "Y");
    boolean flag = true;

    try {
        request.setCharacterEncoding("UTF-8");

        String postfixName = (String) request.getParameter("postfixName");// 服务器前缀
        if (MyStringUtil.isBlank(postfixName)) {
            postfixName = "";
        }


        BufferedReader br = request.getReader();
        String str = "";
        String listString = "";
        while ((str = br.readLine()) != null) {
            listString += str;
        }

        Object o = JSON.parseArray(listString);
        List<Map<String, String>> newobjectParam = (List<Map<String, String>>) o;

        int logoIndex = newobjectParam.size() - 1;
        //测试
        //String logoPath = "D:/qrcodeTest";
        String basePath = request.getServletContext().getRealPath("/");
        String logoPath = basePath;
        //获取logo名字
        String logoName = newobjectParam.get(logoIndex).get("logoName");

        for (int i = 0; i < logoIndex; i++) {
            //id
            String id = newobjectParam.get(i).get("id");
            //二维码内容
            String new_pro_code = "";
            if (StringUtils.isNotBlank(newobjectParam.get(i).get("new_pro_code"))) {
                new_pro_code = newobjectParam.get(i).get("new_pro_code");
            }
            //投用日期
            String start_use_time = "";
            if (newobjectParam.get(i).get("start_use_time") != null) {
                Long time = Long.valueOf(String.valueOf(newobjectParam.get(i).get("start_use_time")));
                Date date = new Date(time);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                start_use_time = sdf.format(date);
            }
            //资产名称
            String pro_name = "";
            if (StringUtils.isNotBlank(newobjectParam.get(i).get("pro_name"))) {
                pro_name = newobjectParam.get(i).get("pro_name");
            }
            //获取文件路径
            String filename = UUID.randomUUID().toString().replace("-", "");
            filename = MyStringUtil.getFileName(filename, postfixName);
            String filepath = MyStringUtil.getFilePath(filename);
            File f = new File(basePath + filepath);
            if (!f.exists()) {
                f.mkdirs();
            }

            String filenames = filename + ".jpg";// 文件名
            String path = basePath + filepath + "/" + filename + ".jpg";// 文件绝对路径

           /* String title = "名称：" + pro_name;
            String new_pro_code_text = "编码：" + new_pro_code;
            String text2 = "购置：" + start_use_time;*/

            String title = pro_name;
            String new_pro_code_text = new_pro_code;
            String text2 = start_use_time;

            //绘制二维码
            if (new_pro_code != null && new_pro_code.trim() != null) {
                WriteQrCode.writeTextCode(new_pro_code, title, new_pro_code_text, text2, path);
                System.out.println(basePath + filepath + "/" + filename + ".jpg");
            }
            /*Map<String,String> idAndName = new HashMap<>();
            idAndName.put("filename", filenames);*/
            //不返回图片名字
            obj.put(id, filenames);
        }
    } catch (Exception e) {
        obj.put("errMsg", e);
        obj.put("result", "N");
        e.printStackTrace();
    } finally {
        try {

        } catch (Exception e) {
        }
        //out.println("Access-Control-Allow-Origin:*\r\n");
        String strObj = obj.toString();
        out.println(strObj);
    }
%>
